package top.yuhaitao.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;

import top.yuhaitao.cloud.entity.User;
import top.yuhaitao.cloud.repository.UserRepository;

@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private EurekaClient eurekaClient;

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/getUserById")
	public User getUserById(Long id) {
		return this.userRepository.findOne(id);
	}

	@RequestMapping("/getUserById2/{id}")
	public User getUserById2(@PathVariable("id") Long id) {
		return this.userRepository.findOne(id);
	}

	@RequestMapping("/user")
	public User postUser(@RequestBody User user) {
		return user;
	}

	@RequestMapping("/getUser")
	public User getUser(User user) {
		return user;
	}

	//获取application的url
	@GetMapping("/eureka-instance")
	public String serviceUrl() {
		InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("SPRINGBOOT-CLOUD-PROVIDER-USER", false);
		return instance.getHomePageUrl();
	}

	@GetMapping("/instance-info")
	public ServiceInstance showInfo() {
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}

}
